package com.alpha.processor.processor.anno_processor;

import com.alpha.binder.anno.processor.DataProcessor;
import com.alpha.processor.processor.tools.ProcessorTool;
import com.alpha.processor.creators.connector_file.ConnectorFileCreator;
import com.alpha.processor.processor.tools.AnnoProcessorToolException;
import com.alpha.processor.processor.anno_processor.base.AnnoProcessor;

import java.util.Map;
import java.util.Set;

import javax.annotation.processing.Messager;
import javax.annotation.processing.RoundEnvironment;
import javax.lang.model.element.Element;
import javax.lang.model.element.ElementKind;
import javax.lang.model.element.TypeElement;
import javax.lang.model.util.Elements;

public class DataProcessorAnnoProcessor extends AnnoProcessor {

    private final Map<String, ConnectorFileCreator> connectorFileCreatorMap;

    public DataProcessorAnnoProcessor(Messager messager, Elements elementUtils, ProcessorTool logger,
                                      Map<String, ConnectorFileCreator> connectorFileCreatorMap) {
        super(messager, elementUtils, logger);
        this.connectorFileCreatorMap = connectorFileCreatorMap;
    }

    @Override
    public void processAnno(RoundEnvironment roundEnvironment) {
        Set<? extends Element> elementSet = roundEnvironment.getElementsAnnotatedWith(DataProcessor.class);
        for (Element element : elementSet) {
            TypeElement classElement = (TypeElement) element.getEnclosingElement();
            String fullClassName = classElement.getQualifiedName().toString();
            ConnectorFileCreator fileCreator = connectorFileCreatorMap.get(fullClassName);
            if (fileCreator == null) {
                fileCreator = new ConnectorFileCreator(logger, elementUtils, classElement);
                connectorFileCreatorMap.put(fullClassName, fileCreator);
            }
            fileCreator.getCreateModel().getProcessorInfoModel().updateProcessorElement(element);
        }
    }
}
